/*
* ============================================================================
*  Name        : sea_tree.h
*  Part of     : Finite state machine utilities
*  Description : Tree object implementation
*  Version     : %version: %
*  
*
* ============================================================================
*/
/**
 * @file sea_tree.h
 * @brief
 * @ref sea::Link based @ref sea::Tree
 *
 * @ingroup AssociationUtilities
 */
#if !defined( __sea_tree_h__ )
#define __sea_tree_h__

#include "sea_list.h"

namespace sea
{
    /** 
     * @class Tree sea_tree.h "sea_tree.h"
     * @brief 
     * Tree object. Implements tree objects associations. \b NOT \b Thread \b safe.
     *
     * @ingroup AssociationUtilities
     */
    class Tree: public Object
    {
    public:

        /**
         * Constructor
         */
        Tree( Object* root_obj = NULL );
        virtual ~Tree( );

        /**
         * Get root object
         *
         * @return root object pointer
         */
        sea::ref< > root( ){ return m_root; }

        /**
         * Attach a child to a parent by specified ID
         *
         * @param parent   Parent object
         * @param child    Child object
         * @param id       Association Id
         *
         * @return @li true     if object planted successfully
         * @return @li false    otherwise.
         */
        virtual bool attach( Object* parent, Object* child, Association id = 0 );
        virtual bool attach( ref< >& parent, ref< >& child, Association id = 0 ){ return attach( parent.get( ), child.get( ), id ); }

        /**
         * Detach branch specified by an id
         * If id != NULL then branch is specified
         * 
         * only branch hanging out from the object latter will be preserved, unless 
         * there is only one object in the tree.
         *
         * @param obj  Object to detach from
         * @param id   Association Id
         *
         * @return @li true     on success
         * @return @li false    otherwise
         */
        virtual bool detach_branch( Object* obj, Association id );
        virtual bool detach_branch( ref< >& obj, Association id ){ return detach_branch( obj.get( ), id ); }

        /**
         * Detach all branches from specified object
         * All branches grown from the object will be destroyed 
         * including object itself. 
         *
         * @param obj  Object to detach from
         *
         * @return @li true     on success
         * @return @li false    otherwise
         */
        virtual bool detach_all_branches( Object* obj ) { return detach_branch( obj, -1 ); }
        virtual bool detach_all_branches( ref< >& obj ) { return detach_all_branches( obj.get( ) ); }

    private:
        sea::ref< > m_root;     ///< Root element
    };
}

#endif // #if !defined( __sea_tree_h__ )
